package com.xtoneict.auth.controller;

import com.xtoneict.auth.entity.Holiday;
import com.xtoneict.auth.service.IHolidayService;
import com.xtoneict.util.result.Result;
import com.xtoneict.util.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 关于休假和工作日的Controller
 */
@RestController
@RequestMapping("/holiday")
public class HolidayController {
    @Autowired
    private IHolidayService holidayService;

    /**
     * 更新每年休假情况
     * @param year
     * @return
     */
    @RequestMapping("/getHolidayForYear")
    public Result getHolidayForYear(Integer year) throws IOException {
        if (year == null){
            System.out.println("传入数据为空");
        }
        System.out.println(year);
        for (int i = 1; i <= 12; i++){
            DateUtil dateUtil = new DateUtil();
            int day = dateUtil.getMonthCount(year, i);

            for (int j = 1;j <= day; j++){
                char status = dateUtil.judgeIfHoliday(year, i, j);

                Holiday holiday = new Holiday();
                holiday.setYear(year);
                holiday.setMonth(i);
                holiday.setDay(j);
                holiday.setStatus(status);

                holidayService.save(holiday);
            }
        }

        return Result.ok();
    }

    @RequestMapping("/judgeIfHoliday")
    public char judgeIfHoliday(Integer year, Integer month, Integer day){
        return holidayService.selectStatusByDate(year, month, day);
    }
}
